package com.kadron.creator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.kadron.dao.KImageDAO;
import com.kadron.dao.KItemTypeDAO;
import com.kadron.datamodel.KModelCreator;
import com.kadron.entity.ItemType;
import com.kadron.entity.KItemImage;
import com.kadron.itemtype.KItemTypeModel;

public class KItemTtypeModelCreator implements KModelCreator {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1100860778430845237L;
	
	private static final Log log = LogFactory.getLog(KItemTtypeModelCreator.class);
	
	private KItemTypeDAO itemTypeDAO;
	private KImageDAO imageDAO;
	
	public KItemTtypeModelCreator(KItemTypeDAO itemTypeDAO, KImageDAO imageDAO) {
		super();
		this.itemTypeDAO = itemTypeDAO;
		this.imageDAO = imageDAO;
	}

	@Override
	public KItemTypeModel create(long id) {
		if (id <= 0) {
			log.error("id is <= 0");
			return null;
		}
		ItemType itemType = (ItemType) itemTypeDAO.find(id, ItemType.class);
		KItemImage img = imageDAO.getMainItemImage(itemType.getId());
		if (img == null) {
			img = new KItemImage(itemType.getId());
		}
		KItemTypeModel model = new KItemTypeModel(itemType,img);
		return model;
	}

}
